package com.xiaozhengkeji.mc.plugins.cdkey.commands;


import com.xiaozhengkeji.mc.plugins.cdkey.base.BaseFactory;
import com.xiaozhengkeji.mc.plugins.cdkey.base.SQLiteFactory;
import com.xiaozhengkeji.mc.plugins.cdkey.config.HelpConfig;
import com.xiaozhengkeji.mc.plugins.cdkey.entitys.CDKeyEntity;
import com.xiaozhengkeji.mc.plugins.cdkey.utils.DaysUtil;
import com.xiaozhengkeji.mc.plugins.cdkey.utils.MsgUtils;
import com.xiaozhengkeji.mc.plugins.cdkey.utils.RandomUtils;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;


public class AdminCommands implements CommandExecutor {
    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (!(sender instanceof Player)) {
            MsgUtils.logServer("请在客户端使用!");
            return true;
        }
        Player p = (Player) sender;
        if (!p.isOp()) {
            MsgUtils.logServer("无权限");
            return true;
        }

        if (args.length == 0) {
            HelpConfig.Admin_Help.forEach(help -> MsgUtils.logPlayer(p, help));
            return true;
        } else if (args[0].equalsIgnoreCase("new")) {
            int rets = 0;
            for (int i = 0; i < Integer.valueOf(args[2]); i++) {
                CDKeyEntity cdKeyEntity = new CDKeyEntity(
                        null,
                        RandomUtils.randomString(16),
                        p.getUniqueId().toString(),
                        DaysUtil.getDate(),
                        args[1], args[3], "", ""
                );

                rets = rets + new SQLiteFactory().addCDKey(cdKeyEntity);

            }
            MsgUtils.logPlayer(p, "生成完毕!->" + String.valueOf(rets));
            return true;
        } else if (args[0].equalsIgnoreCase("exp")) {
            List<String> cdks = new LinkedList<>();
            if (args[1].equalsIgnoreCase("ps")) {
                List<CDKeyEntity> cdKeyEntities = new SQLiteFactory().getCDKeyByPostscript(args[2]);
                cdKeyEntities.forEach(cdk -> cdks.add(cdk.cdkey));
            } else if (args[1].equalsIgnoreCase("cr")) {
                List<CDKeyEntity> cdKeyEntities = new SQLiteFactory().getCDKeyByCreator(Bukkit.getPlayer(args[2]).getUniqueId().toString());
                cdKeyEntities.forEach(cdk -> cdks.add(cdk.cdkey));
            }
            new BaseFactory().expText(cdks);
            MsgUtils.logPlayer(p, "导出完毕!");
            return true;
        } else if (args[0].equalsIgnoreCase("info")) {

            CDKeyEntity cdKeyEntity = new SQLiteFactory().getCDKeyByCDKey(args[1]);
            MsgUtils.logPlayer(p, "CDKey: " + cdKeyEntity.cdkey);
            MsgUtils.logPlayer(p, "创建者: " + Bukkit.getPlayer(UUID.fromString(cdKeyEntity.up_creator)).getName());
            MsgUtils.logPlayer(p, "创建时间: " + cdKeyEntity.up_date);
            MsgUtils.logPlayer(p, "绑定礼包: " + cdKeyEntity.b_commands);
            MsgUtils.logPlayer(p, "备注: " + cdKeyEntity.b_postscript);
            if (cdKeyEntity.open_player.equalsIgnoreCase("")) {
                MsgUtils.logPlayer(p, "未激活");
            } else {
                MsgUtils.logPlayer(p, "激活者: " + Bukkit.getPlayer(UUID.fromString(cdKeyEntity.open_player)).getName());
                MsgUtils.logPlayer(p, "激活时间: " + cdKeyEntity.open_date);
            }


        }

        return true;
    }
}
